import selenium
from selenium import webdriver
import pathlib
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

# 基本信息
# 视频存放路径
catalog_mp4 = r"C:\Users\Jingbit\Desktop\自媒体"
# 视频描述
describe = "裸眼3D看蜘蛛侠 #搞笑 #电影 #视觉震撼"
time.sleep(10)
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:5003")
driver = webdriver.Chrome(options=options)

path = pathlib.Path(catalog_mp4)

# 视频地址获取
path_mp4 = ""
for i in path.iterdir():
    if ".mp4" in str(i):
        path_mp4 = str(i);
        break;

if path_mp4 != "":
    print("检查到视频路径：" + path_mp4)
else:
    print("未检查到视频路径，程序终止！")
    exit()

# 封面地址获取
path_cover = ""
for i in path.iterdir():
    if ".png" in str(i) or ".jpg" in str(i):
        path_cover = str(i);
        break;

if path_cover != "":
    print("检查到封面路径：" + path_cover)
else:
    print("未检查到封面路径，程序终止！")
    exit()


def publish_haokan():
    '''
     作用：发布好看视频
    '''

    # 进入创作者页面，并上传视频
    driver.get("https://dream.haokan.com/author/upload")
    time.sleep(2)
    driver.find_element(By.XPATH, '//input[@type="file"]').send_keys(path_mp4)

    # 等待视频上传完成
    while True:
        time.sleep(3)
        try:
            driver.find_element(By.XPATH, '//*[text()="上传成功"]')
            break;
        except Exception as e:
            print("视频还在上传中···")

    print("视频已上传完成！")

    # 选择分类
    driver.find_element(By.XPATH, '//*[@class="hk-select-selection-item"]').click()
    time.sleep(2)
    driver.find_element(By.XPATH, '//*[@title="影视"]').click()
    time.sleep(1)

    # 添加封面
    driver.find_element(By.XPATH, '//*[text()="上传封面"]').click()
    time.sleep(5)
    driver.find_element(By.XPATH, '//*[text()="本地上传"]').click()
    time.sleep(1)
    driver.find_element(By.XPATH, '//*[@class="image-uploader-container"]//input[@type="file"]').send_keys(path_cover)
    time.sleep(5)
    driver.find_element(By.XPATH, '//*[text()="完成"]').click()

    time.sleep(2)
    # 输入标题
    driver.find_element(By.XPATH, '//*[@class="input-content"]//input').send_keys(Keys.CONTROL, 'a')
    time.sleep(2)
    driver.find_element(By.XPATH, '//*[@class="input-content"]//input').send_keys(describe)

    # 输入视频描述
    driver.find_element(By.XPATH, '//textarea').send_keys(describe)

    # 位置
    time.sleep(1)
    driver.find_element(By.XPATH, '//*[@class="location-input-wrap"]//input').send_keys("中关村人工智能科技园")
    time.sleep(3)
    driver.find_element(By.XPATH, '//*[text()="中关村人工智能科技园"]').click()

    # 参加话题
    time.sleep(1)
    driver.find_element(By.XPATH, '//*[text()="展开"]').click()
    time.sleep(1)
    driver.find_element(By.XPATH, '//*[text()="好看创作季"]').click()
    time.sleep(1)

    # 人工进行检查并发布
    # time.sleep(3)
    # # 点击发布
    # driver.find_element(By.XPATH,'//*[text()="发布"]').click()


# 开始执行视频发布
publish_haokan()
